package com.twu.biblioteca;

import java.util.List;

import static com.twu.biblioteca.InputReader.*;

/**
 * Created by utsabban on 2/25/14.
 */
public class Customer {
    private String customerNumber;


    Customer(String customerNumber)
    {
        this.customerNumber = customerNumber;
    }

    public String showMainMenu()
    {
        return "1.List Books\n2.Checkout Book\n3.Return Book\n4.List Movies\n5.Checkout Movie\n6.Return Movie\n7.Display My User Information\n8.Logout";
    }

    public String chooseMainMenuOption()
    {
        return readInput();
    }

    public Integer validateMainMenuOption(String selectedMainMenuOption)
    {
        Integer mainMenuOption=null;
        try {
            mainMenuOption = Integer.parseInt(selectedMainMenuOption);
            if(mainMenuOption<1 || mainMenuOption>8)
                mainMenuOption=null;
            }
            catch (NumberFormatException e)
            {
                //e.printStackTrace();
                mainMenuOption=null;
            }
        return mainMenuOption;
    }

    public String checkoutBook(Library library,String bookToCheckout)
    {
        List<Book> bookList=library.getBookList();
        boolean successfulCheckout=false;
        for(Book book:bookList)
        {
            if(book.getBookName().equals(bookToCheckout))
            {
                //bookList.remove(book);
                successfulCheckout=book.bookCheckout(bookToCheckout, customerNumber);
                //bookList.add(book);
            }
        }
        if(successfulCheckout)
            return "Thank you! Enjoy "+bookToCheckout+"!";
        else
            return "Sorry! "+bookToCheckout+" is currently not available/ not available at all!";
    }

    public String checkoutMovie(Library library,String movieToCheckout)
    {
        List<Movie> movieList=library.getMovieList();
        boolean successfulCheckout=false;
        for(Movie movie:movieList)
        {
            if(movie.getMovieName().equals(movieToCheckout))
                successfulCheckout=movie.movieCheckout(movieToCheckout,customerNumber);
        }
        if(successfulCheckout)
            return "Thank you! Enjoy "+movieToCheckout+"!";
        else
            return "Sorry! "+movieToCheckout+" is currently not available/ not available at all!";
    }

    public String returnBook(Library library,String bookToReturn)
    {
        List<Book> bookList=library.getBookList();
        boolean successfulReturn=false;
        for(Book book:bookList)
        {
            if(book.getBookName().equals(bookToReturn))
            {
                //bookList.remove(book);
                successfulReturn=book.bookReturn(bookToReturn, customerNumber);
                //bookList.add(book);
            }
        }
        if(successfulReturn)
            return "Thank you for returning "+bookToReturn+"!";
        else
            return "Sorry! "+bookToReturn+" does not belong to the library/ not borrowed by the current user!";
    }

    public String returnMovie(Library library,String movieToReturn)
    {
        List<Movie> movieList=library.getMovieList();
        boolean successfulReturn=false;
        for(Movie movie:movieList)
        {
            if(movie.getMovieName().equals(movieToReturn))
                successfulReturn=movie.movieReturn(movieToReturn,customerNumber);
        }
        if(successfulReturn)
            return "Thank you for returning "+movieToReturn+"!";
        else
            return "Sorry! "+movieToReturn+" does not belong to the library/ not borrowed by the current user!";
    }

    public String logout()
    {
        return "Thank you! Visit again!";
    }
}
